//package com.dongzili.postgresql.common.mybatisplus;
//
//import org.apache.ibatis.executor.statement.StatementHandler;
//import org.apache.ibatis.mapping.BoundSql;
//import org.apache.ibatis.mapping.MappedStatement;
//import org.apache.ibatis.mapping.SqlCommandType;
//import org.apache.ibatis.plugin.*;
//import org.apache.ibatis.reflection.MetaObject;
//import org.apache.ibatis.reflection.SystemMetaObject;
//import java.lang.reflect.Field;
//import java.sql.Connection;
//import java.util.Arrays;
//import java.util.HashSet;
//import java.util.Properties;
//import java.util.Set;
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
//
//@Intercepts({@Signature(
//    type = StatementHandler.class,
//    method = "prepare",
//    args = {Connection.class, Integer.class}
//)})
//public class PostgreSqlFieldNameInterceptor implements Interceptor {
//
//
//    private static final Pattern FIELD_PATTERN = Pattern.compile("(?<!\\w)([a-zA-Z_][a-zA-Z0-9_]*)(?!\\w)");
//
//    @Override
//    public Object intercept(Invocation invocation) throws Throwable {
//        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
//        MetaObject metaObject = SystemMetaObject.forObject(statementHandler);
//        MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
//        SqlCommandType commandType = mappedStatement.getSqlCommandType();
//
//        // 兼容低版本，不使用 isDml()
//        boolean isDmlOrSelect = commandType == SqlCommandType.SELECT
//                || commandType == SqlCommandType.INSERT
//                || commandType == SqlCommandType.UPDATE
//                || commandType == SqlCommandType.DELETE;
//
//        if (isDmlOrSelect) {
//            BoundSql boundSql = statementHandler.getBoundSql();
//            String sql = boundSql.getSql();
//
//            Matcher matcher = FIELD_PATTERN.matcher(sql);
//            StringBuffer newSql = new StringBuffer();
//            while (matcher.find()) {
//                String field = matcher.group(1);
//                if (!field.matches("^\\d+$") && !isKeyword(field) && !sql.contains("\"" + field + "\"")) {
//                    matcher.appendReplacement(newSql, "\"" + field + "\"");
//                } else {
//                    matcher.appendReplacement(newSql, field);
//                }
//            }
//            matcher.appendTail(newSql);
//
//            // 反射修改 SQL（避免直接调用 setSql()）
//            Field sqlField = BoundSql.class.getDeclaredField("sql");
//            sqlField.setAccessible(true);
//            sqlField.set(boundSql, newSql.toString());
//        }
//
//        return invocation.proceed();
//    }
//
//    // 过滤 SQL 关键字
//    private boolean isKeyword(String word) {
//        String[] keywords = {"select", "as", "from", "where", "and", "or", "order", "by", "limit", "offset", "insert", "into", "update", "delete", "set", "values", "count", "sum", "avg", "max", "min", "group", "join", "on", "left", "right", "inner", "outer", "not", "null", "true", "false", "between", "in", "like", "escape", "union", "all", "distinct", "exists", "any", "some"};
//        for (String kw : keywords) {
//            if (kw.equalsIgnoreCase(word)) return true;
//        }
//        return false;
//    }
//
//    @Override
//    public Object plugin(Object target) {
//        return Plugin.wrap(target, this);
//    }
//
//    @Override
//    public void setProperties(Properties properties) {}
//}